#!/bin/bash

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 项目目录
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$PROJECT_DIR" || exit 1

echo -e "${BLUE}================================================"
echo -e "    建党在线系统 - 暂停指定服务"
echo -e "================================================${NC}\n"

# 检查 Docker 是否运行
if ! docker info > /dev/null 2>&1; then
    echo -e "${RED}❌ Docker 未运行，请先启动 Docker Desktop${NC}"
    exit 1
fi

# 检查 docker-compose.yml 是否存在
if [ ! -f "docker-compose.yml" ]; then
    echo -e "${RED}❌ 未找到 docker-compose.yml 文件${NC}"
    exit 1
fi

# 服务列表
services=("backend" "client" "front")
service_names=("后端服务 (backend)" "管理端 (client)" "用户端 (front)")

# 显示服务选择菜单
echo -e "${YELLOW}请选择要暂停的服务：${NC}"
for i in "${!services[@]}"; do
    index=$((i + 1))
    echo "  $index) ${service_names[$i]}"
done
echo ""

# 读取用户选择
read -p "请选择 (1-3): " choice

# 验证输入
if ! [[ "$choice" =~ ^[1-3]$ ]]; then
    echo -e "${RED}❌ 无效的选择${NC}"
    exit 1
fi

# 获取选择的服务
index=$((choice - 1))
selected_service="${services[$index]}"
selected_name="${service_names[$index]}"

echo -e "\n${BLUE}🛑 正在暂停 ${selected_name}...${NC}"

# 暂停服务
if docker compose stop "$selected_service"; then
    echo -e "${GREEN}✅ ${selected_name} 已成功暂停！${NC}"
    
    # 显示服务状态
    echo -e "\n${BLUE}📊 当前服务状态：${NC}"
    docker compose ps
    
    echo -e "\n${YELLOW}💡 提示：${NC}"
    echo -e "  • 启动该服务: ${GREEN}docker compose start $selected_service${NC}"
    echo -e "  • 查看所有服务: ${GREEN}docker compose ps${NC}"
else
    echo -e "${RED}❌ 暂停服务失败！${NC}"
    exit 1
fi

